#!/usr/bin/python

# Used to create objects in objects/hosts
import sys

if len(sys.argv) != 2:
	print "must use make-host-template.py <host>"
host = sys.argv[1]


from nagiosconfig import host_base, service_base
print """
define host{
	use			%s
	host_name		%s
	alias			%s
	address			%s
}""" % tuple([host_base[host]] + [host]*3)
print """
define hostextinfo{
        host_name     	%s 
        #notes           Large Memory Machine
        #notes_url       http://mirrors.rit.edu
        icon_image      base/redhat.jpg
        icon_image_alt  Server
        #vrml_image      nagiosvrml.png
        #statusmap_image        novell40.gd2
        #2d_coords       100,250
        #3d_coords       100.0,50.0,75.0
}""" % host


from nagiosconfig import DEFAULTS, EXCEPTIONS, SERVICE_NAME, \
                         CHECK_COMMAND, LIMIT_TO
from nagiosconfig import nix_service_parameters as p
from nagiosconfig import win_service_parameters as win_p
from nagiosconfig import all_nix_hosts, all_win_hosts

if not host in all_win_hosts:
    # Handle the SSH check separately, but only if not windows
    print """
    define service{
            use                             %s
            host_name                       %s
        	normal_check_interval		60 # every hour
            service_description             SSH
            check_command                   check_ssh
    }""" % (service_base[host], host)

os_pairs = [
    [p, all_nix_hosts],
    [win_p, all_win_hosts],
]
for parm_dict, host_list in os_pairs:
    for service_key, parms in parm_dict.iteritems():
        if not host in parms.get(LIMIT_TO, host_list):
            continue
        sname = parms[SERVICE_NAME]
        cmd   = parms[CHECK_COMMAND] % \
                tuple(parms[EXCEPTIONS].get(host, parms[DEFAULTS]))
        print """
            define service{
                use                 %s 
                host_name           %s
                service_description %s
                check_command       %s
            }""" % (service_base[host], host, sname, cmd)

# Only for sql servers
from nagiosconfig import sql_servers
if host in sql_servers:
	for entry in sql_servers[host]:
		u, p, addr, port, db = entry
		print """
define service{
	use				%s
	host_name			%s
	service_description		MySQL DB access %s - %s 
	check_command			check_nrpe!check_mysql!%s!%s!%s!%s!%s
}""" % (service_base[host], host, addr, db, db, addr, port, u, p)

# All web hosts and web sites
from nagiosconfig import web_servers
if host in web_servers.keys():
	for pair in web_servers[host]:
		ssl, site = pair
		if ssl and (site != "trac.rc.rit.edu" and site != "stats.rc.rit.edu"):
			print """
define service{
	use				%s
	host_name			%s
	service_description	        %s
	check_command   		check_nrpe!check_http_ssl!5!10!%s
}
""" % (service_base[host], host, 'Access_w_ssl_to_%s' % site, site)
		if ssl:
			print """
define service{
	use				%s
	host_name			%s
	service_description	        %s
	check_command   		check_nrpe!check_http_cert!%s!14
}
""" % (service_base[host], host, 'Certificate valid for %s' % site, site)
		else: # not ssl
			print """
define service
	use				%s
	host_name			%s
	service_description	        %s
	check_command   		check_nrpe!check_http!5!10!%s
}
""" % (service_base[host], host, 'Access_to_%s' % site, site)
			




